home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / brailler-04b-c / brlr ƒ / Shell ƒ / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  6.7 KB  |  273 lines  |  [TEXT/MMCC]

  1. #include "help.h"
  2. #include "environment.h"
  3. #include "popup.h"
  4. #include "util.h"
  5. #include "buttons.h"
  6. #include "styled text.h"
  7. #include "program globals.h"
  8. #include "window layer.h"
  9. #include "graphics.h"
  10.  
  11. #define DEAD_SPACE_TOP        10
  12. #define DEAD_SPACE_LEFT        10
  13. #define DEAD_SPACE_BOTTOM    5
  14. #define DEAD_SPACE_RIGHT    10
  15. #define    TEXT_RECT_WIDTH        405
  16. #define    TEXT_RECT_HEIGHT    250
  17. #define    BUTTON_WIDTH        80
  18. #define    BUTTON_HEIGHT        17
  19. #define BUTTON_GAP_H        15
  20. #define BUTTON_GAP_V        5
  21.  
  22. #define MAX_MAIN_TOPICS        5
  23. #define    MAX_SUB_TOPICS        6
  24.  
  25. #define MAIN_TOPIC_ID        600
  26. #define POPUP_MENU_ID        100
  27.  
  28. short            gMainTopicShowing;        /* saved in prefs file */
  29. short            gSubTopicShowing;        /* saved in prefs file */
  30.  
  31. static    short            gNumMainTopics;
  32. static    short            gNumSubTopics[MAX_MAIN_TOPICS];
  33. static    Str31            gMainTopicTitle[MAX_MAIN_TOPICS];
  34. static    Rect            gMainTopicRect[MAX_MAIN_TOPICS];
  35. static    Str31            gSubTopicTitle[MAX_MAIN_TOPICS][MAX_SUB_TOPICS];
  36. static    short            gSubTopicID[MAX_MAIN_TOPICS][MAX_SUB_TOPICS];
  37. static    Rect            gTextRect;
  38. static    CharHandle        gTheText;
  39. static    StylHandle        gTheStyle;
  40.  
  41. static    Boolean            gSetupDone=FALSE;
  42.  
  43. /*-----------------------------------------------------------------------------------*/
  44. /* internal stuff for help.c                                                         */
  45.  
  46. static    short ParseRawTitle(Str255 theTitle);
  47. static    void GoToPage(WindowPtr theWindow, short mainTopic, short subTopic,
  48.                 Boolean updateNow);
  49. static    void GetTextResources(short mainTopic, short subTopic);
  50. static    void DisposeTextResources(void);
  51.  
  52. void SetupTheHelpWindow(WindowPtr theWindow)
  53. {
  54.     short            i,j;
  55.     unsigned char    *titleStr="\pHelp";
  56.     Handle            temp;
  57.     short            strID;
  58.     Point            thePoint;
  59.     
  60.     SetWindowMaxDepth(theWindow, 8);
  61.     SetWindowDepth(theWindow, 8);
  62.     SetWindowWidth(theWindow, DEAD_SPACE_LEFT+TEXT_RECT_WIDTH+DEAD_SPACE_RIGHT);
  63.     SetWindowHeight(theWindow, BUTTON_GAP_V+DEAD_SPACE_TOP+BUTTON_HEIGHT+TEXT_RECT_HEIGHT+
  64.         DEAD_SPACE_BOTTOM);
  65.     SetWindowType(theWindow, noGrowDocProc);
  66.     SetWindowHasCloseBox(theWindow, TRUE);
  67.     thePoint.v=50;
  68.     thePoint.h=6;
  69.     SetWindowTopLeft(theWindow, thePoint);
  70.     SetWindowIsFloat(theWindow, FALSE);
  71.     SetWindowTitle(theWindow, titleStr);
  72.     SetWindowAutoCenter(theWindow, FALSE);
  73.     
  74.     if (gSetupDone)
  75.         return;
  76.     
  77.     temp=GetResource('STR#', MAIN_TOPIC_ID);
  78.     gNumMainTopics=**((short**)temp);
  79.     ReleaseResource(temp);
  80.     for (i=0; i<gNumMainTopics; i++)
  81.     {
  82.         GetIndString(gMainTopicTitle[i], MAIN_TOPIC_ID, i+1);
  83.         strID=ParseRawTitle(gMainTopicTitle[i]);
  84.         
  85.         for (j=0; j<5; j++)
  86.             gMainTopicTitle[i][++gMainTopicTitle[i][0]]=' ';
  87.         
  88.         SetRect(    &gMainTopicRect[i],
  89.                     DEAD_SPACE_LEFT+(BUTTON_WIDTH+BUTTON_GAP_H)*i,
  90.                     DEAD_SPACE_TOP,
  91.                     DEAD_SPACE_LEFT+(BUTTON_WIDTH+BUTTON_GAP_H)*i+BUTTON_WIDTH,
  92.                     DEAD_SPACE_TOP+BUTTON_HEIGHT);
  93.         
  94.         temp=GetResource('STR#', strID);
  95.         gNumSubTopics[i]=**((short**)temp);
  96.         ReleaseResource(temp);
  97.         
  98.         for (j=0; j<gNumSubTopics[i]; j++)
  99.         {
  100.             GetIndString(gSubTopicTitle[i][j], strID, j+1);
  101.             gSubTopicID[i][j]=ParseRawTitle(gSubTopicTitle[i][j]);
  102.         }
  103.     }
  104.     
  105.     gTheText=0L;
  106.     gTheStyle=0L;
  107.     GoToPage(0L, gMainTopicShowing, gSubTopicShowing, FALSE);
  108.     
  109.     SetRect(&gTextRect, DEAD_SPACE_LEFT, DEAD_SPACE_TOP+BUTTON_HEIGHT+BUTTON_GAP_V,
  110.         DEAD_SPACE_LEFT+TEXT_RECT_WIDTH,
  111.         DEAD_SPACE_TOP+BUTTON_HEIGHT+BUTTON_GAP_V+TEXT_RECT_HEIGHT);
  112.     
  113.     gSetupDone=TRUE;
  114. }
  115.  
  116. void ShutDownTheHelpWindow(void)
  117. {
  118.     DisposeTextResources();
  119. }
  120.  
  121. void KeyPressedInHelpWindow(WindowPtr theWindow, unsigned char keyPressed)
  122. {
  123.     ObscureCursor();
  124.     
  125.     switch (keyPressed)
  126.     {
  127.         case 0x1d:                                        /* right arrow */
  128.             gSubTopicShowing++;
  129.             if (gSubTopicShowing>=gNumSubTopics[gMainTopicShowing])
  130.             {
  131.                 gSubTopicShowing=0;
  132.                 gMainTopicShowing++;
  133.                 if (gMainTopicShowing>=gNumMainTopics)
  134.                     gMainTopicShowing=0;
  135.             }
  136.             GoToPage(theWindow, gMainTopicShowing, gSubTopicShowing, TRUE);
  137.             break;
  138.         case 0x1c:                                        /* left arrow */
  139.             gSubTopicShowing--;
  140.             if (gSubTopicShowing<0)
  141.             {
  142.                 gMainTopicShowing--;
  143.                 if (gMainTopicShowing<0)
  144.                     gMainTopicShowing=gNumMainTopics-1;
  145.                 gSubTopicShowing=gNumSubTopics[gMainTopicShowing]-1;
  146.             }
  147.             GoToPage(theWindow, gMainTopicShowing, gSubTopicShowing, TRUE);
  148.             break;
  149.     }
  150. }
  151.  
  152. void MouseClickedInHelpWindow(WindowPtr theWindow, Point mouseLoc)
  153. {
  154.     short            i;
  155.     short            newMain, newSub;
  156.     MenuHandle        theMenu;
  157.     Rect            menuRect;
  158.     
  159.     newMain=-1;
  160.     
  161.     for (i=0; i<gNumMainTopics; i++)
  162.     {
  163.         if (PtInRect(mouseLoc, &gMainTopicRect[i]))
  164.         {
  165.             newMain=i;
  166.             i=gNumMainTopics;
  167.         }
  168.     }
  169.     
  170.     if (newMain!=-1)
  171.     {
  172.         Draw3DButton(&gMainTopicRect[newMain], gMainTopicTitle[newMain], 0L,
  173.             GetWindowDepth(theWindow), TRUE, TRUE);
  174.         
  175.         theMenu=NewMenu(POPUP_MENU_ID, "\p");
  176.         for (i=0; i<gNumSubTopics[newMain]; i++)
  177.         {
  178.             AppendMenu(theMenu, gSubTopicTitle[newMain][i]);
  179.             CheckItem(theMenu, i+1, ((newMain==gMainTopicShowing) && (i==gSubTopicShowing)));
  180.         }
  181.         
  182.         menuRect.top=gMainTopicRect[newMain].bottom-1;
  183.         menuRect.left=gMainTopicRect[newMain].left+1;
  184.         newSub=-1;
  185.         if (MouseInModelessPopUp(theMenu, &newSub, &menuRect, POPUP_MENU_ID))
  186.         {
  187.             GoToPage(theWindow, newMain, newSub-1, TRUE);
  188.         }
  189.         else
  190.         {
  191.             Draw3DButton(&gMainTopicRect[newMain], gMainTopicTitle[newMain], 0L,
  192.                 GetWindowDepth(theWindow), FALSE, TRUE);
  193.         }
  194.         
  195.         DisposeHandle((Handle)theMenu);
  196.     }
  197. }
  198.  
  199. void DrawTheHelpWindow(WindowPtr theWindow, short theDepth)
  200. {
  201.     GrafPtr            curPort;
  202.     short            i;
  203.     Rect            tempRect;
  204.         
  205.     curPort=(GrafPtr)theWindow;
  206.     EraseRect(&(curPort->portRect));
  207.     
  208.     DrawTheShadowBox(gTextRect, TRUE);
  209.     if (gTheText!=0L)
  210.     {
  211.         tempRect=gTextRect;
  212.         InsetRect(&tempRect, 8, 4);
  213.         DrawTheText(gTheText, gTheStyle, kLeft, srcOr, tempRect);
  214.     }
  215.     
  216.     for (i=0; i<gNumMainTopics; i++)
  217.     {
  218.         Draw3DButton(&gMainTopicRect[i], gMainTopicTitle[i], 0L, theDepth, FALSE, TRUE);
  219.     }
  220.     
  221. }
  222.  
  223. /* ---------------------------------------- */
  224. /* the rest of these are internal to help.c */
  225.  
  226. static    short ParseRawTitle(Str255 theTitle)
  227. {
  228.     Str255            numStr;
  229.     long            result;
  230.     
  231.     numStr[0]=0x00;
  232.     while ((numStr[numStr[0]]=theTitle[++numStr[0]])!=' ') {}
  233.     theTitle[0]-=numStr[0];
  234.     Mymemcpy((Ptr)&theTitle[1], (Ptr)&theTitle[numStr[0]+1], theTitle[0]);
  235.     numStr[0]--;
  236.     StringToNum(numStr, &result);
  237.     
  238.     return result;
  239. }
  240.  
  241. static    void GoToPage(WindowPtr theWindow, short mainTopic, short subTopic,
  242.                         Boolean updateNow)
  243. {
  244.     DisposeTextResources();
  245.     GetTextResources(mainTopic, subTopic);
  246.     gMainTopicShowing=mainTopic;
  247.     gSubTopicShowing=subTopic;
  248.     if (updateNow)
  249.     {
  250.         UpdateTheWindow(theWindow);
  251.     }
  252. }
  253.  
  254. static    void GetTextResources(short mainTopic, short subTopic)
  255. {
  256.     short            resID;
  257.     
  258.     DisposeTextResources();
  259.     resID=gSubTopicID[mainTopic][subTopic];
  260.     gTheText=(CharHandle)GetResource('TEXT', resID);
  261.     gTheStyle=(StylHandle)GetResource('styl', resID);
  262. }
  263.  
  264. static    void DisposeTextResources(void)
  265. {
  266.     if (gTheText!=0L)
  267.         ReleaseResource((Handle)gTheText);
  268.     if (gTheStyle!=0L)
  269.         ReleaseResource((Handle)gTheStyle);
  270.     gTheText=0L;
  271.     gTheStyle=0L;
  272. }
  273.